<?php
/**
 * File: fashionista.php.
 * Author: zhouzhe@haibao.com
 * Editor: PhpStorm
 * Date: 16/7/1 下午3:37
 * Brief:
 */
namespace haibao\cms\data;

class Fashionista extends \haibao\cms\data\BaseMysql{
	const FASHIONISTA_GROUP_ID = 21;
	public function __construct(){
		$this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_DZ3 );
	}

	public function getBloggerList($conditions){
		$page = $conditions['page'];
		$nickname = isset($conditions['nickname']) ? $conditions['nickname'] : '';
		$uid = isset($conditions['uid']) ? $conditions['uid'] : '';
		$type = isset($conditions['type']) ? $conditions['type'] : 0;
		$where = '';
		if($uid){
			$where .= ' and b.uid='.$uid;
		}else{
			if($nickname){
				$where .= " and b.uname like '%".$nickname."%'";
			}
			if($type){
				$where .= ' and b.cate_id='.$type;
			}
		}
		$data = array();$tag = 0;
		$sql = "SELECT a.*,b.cate_id FROM hb_common_member a left join hb_zj_blogs_cate_user b on a.uid=b.uid  where a.status=0 and a.groupid=%s".$where." order by a.uid desc limit %s,%s";
		$sqlCount = "SELECT count(*) as countnum FROM hb_common_member a left join hb_zj_blogs_cate_user b on a.uid=b.uid  where a.status=0 and a.groupid=%s".$where;
		$result = $this->query($sql,array(self::FASHIONISTA_GROUP_ID,($page-1)*20,$page*20));
		$resultCount = $this->query($sqlCount,array(self::FASHIONISTA_GROUP_ID))->fetch_assoc();
		while($result->fetch_assoc()){
			$data[$tag]['Uid'] = $result->getData('uid');
			$data[$tag]['Username'] = $result->getData('username');
			$data[$tag]['CateId'] = $result->getData('cate_id');
			$tag++;
		}
		return array($data,$resultCount['countnum']);
	}

	public function getBloggerCate(){
		$data = array();
		$sql = "SELECT * FROM hb_zj_blogs_category_old where status=1";
		$result = $this->query($sql);
		while($result->fetch_assoc()){
			$data[$result->getData('id')] = $result->getData('catename');
		}
		return $data;
	}

	public function getBloggerCateInfo(){
		$data = array();
		$sql = "SELECT * FROM hb_zj_blogs_category_old where status=1 order by catelevel asc";
		$result = $this->query($sql);
		while($result->fetch_assoc()){
			$data[$result->getData('id')]['name'] = $result->getData('catename');
			$data[$result->getData('id')]['level'] = $result->getData('catelevel');
			$data[$result->getData('id')]['date'] = date('Y-m-d',$result->getData('pubdate'));
			$data[$result->getData('id')]['parent'] = $result->getData('parent');
		}
		return $data;
	}

	public function deleteBlogger($uid){
		$sqlCateUser = "update hb_zj_blogs_cate_user set status = 0 where uid=%s";
		$sqlMember = "update hb_common_member set status = 1 where uid=%s";
		$this->query($sqlCateUser,array($uid));
		$this->query($sqlMember,array($uid));
	}

	public function editBloggerCate($conditions){
		$sql = "select * from hb_zj_blogs_cate_user where uid=%s";
		$uidExist = $this->query($sql,array($conditions['uid']))->fetch_assoc();
		if($uidExist){
			$sql = "update hb_zj_blogs_cate_user set cate_id=%s where uid=%s";
			$this->query($sql,array($conditions['cate'],$conditions['uid']));
		}else{
			$zimu = $this->getFirstCharter($conditions['name']);
			$addtime = new \DateTime();
			$sql = "insert into hb_zj_blogs_cate_user (cate_id,uid,uname,addtime,zimu,comments_count) values(%s,%s,%s,%s,%s,%s)";
			$this->query($sql,array($conditions['cate'],$conditions['uid'],$conditions['name'],$addtime,$zimu,0));
		}
	}

	public function getInfoList($conditions){
		$data = array();
		$where = ' and a.status=0';
		if(isset($conditions['blogId']) && $conditions['blogId']){
			$where .= ' and a.blogid='.$conditions['blogId'];
		}else{
			if(isset($conditions['uid']) && $conditions['uid']){
				$where .= ' and b.uid='.$conditions['uid'];
			}else{
				if(isset($conditions['bloggerId']) && $conditions['bloggerId']){
					$where .= ' and a.uid='.$conditions['bloggerId'];
				}
			}
			if(isset($conditions['subject']) && $conditions['subject']){
				$where .= " and a.subject like '%".$conditions['subject']."%'";
			}

			if(isset($conditions['cate']) && $conditions['cate']){
				$where .= " and b.cate_id =".$conditions['cate'];
			}

			$startTime = isset($conditions['startDate'])&&$conditions['startDate'] ? strtotime($conditions['startDate']) : 0;
			$endTime = isset($conditions['endDate']) && $conditions['endDate'] ? strtotime($conditions['endDate']) : 0;
			if($startTime){
				$where .= ' and a.dateline >'.$startTime;
			}
			if($endTime && $endTime > $startTime){
				$where .= ' and a.dateline < '.$endTime;
			}
		}

		$limit = ($conditions['page']-1)*20;


		$sql = "select a.*,b.cate_id,b.uname from hb_home_blog a left join hb_zj_blogs_cate_user b on a.uid=b.uid where 1".$where."  order by a.dateline desc limit ".$limit.",20";
		$countSql = "select count(*) as countNum from hb_home_blog a left join hb_zj_blogs_cate_user b on a.uid=b.uid where 1".$where;
		$result = $this->query($sql);
		$resultCount = $this->query($countSql)->fetch_assoc();
		while($result->fetch_assoc()){
			$blogId = $result->getData('blogid');
			$data[$blogId]['uid'] = $result->getData('uid');
			$data[$blogId]['title'] = $result->getData('subject');
			$data[$blogId]['username'] = $result->getData('uname');
			$data[$blogId]['cate'] = $result->getData('cate_id');
			$data[$blogId]['url'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_BOZHU).'/blog/'.$blogId .'.html';
			$data[$blogId]['datetime'] = date('Y-m-d',$result->getData('dateline'));
		}
		return array($data,$resultCount['countNum']);
	}

	public function editBlogTitle($conditions){
		$sql = "update hb_home_blog set subject=%s where blogid=%s";
		$this->query($sql,array($conditions['title'],$conditions['blogid']));
		$sqlCrontab = "insert into hb_home_blog_crontab (`blogid`,`createtime`) value(%s,%s)";
		$this->query($sqlCrontab,array($conditions['blogid'],new \DateTime()));
	}

	public function getPicsByBlogId($blogId){
		$data = $extra = array();
		$sql = "select * from hb_zj_blogs_pic where blogid=%s and status = 1 order by pic_id asc";
		$result = $this->query($sql,array($blogId));
		while($result->fetch_assoc()){
			if($result->getData('type') != 3){
				$data[$result->getData('pic_id')]['FileName'] = $result->getData('filename');
				$data[$result->getData('pic_id')]['DisRel'] = $result->getData('disrel');
			}else{
				$extra[$result->getData('pic_id')]['FileName'] = $result->getData('filename');
				$extra[$result->getData('pic_id')]['DisRel'] = $result->getData('disrel');
			}
		}
		$data = $extra+$data;
		return $data;
	}

	public function setFronPic($blogId,$picId){
		$sqlO = "update hb_zj_blogs_pic set disrel=0 where blogid=%s";
		$sqlT = "update hb_zj_blogs_pic set disrel=1 where blogid=%s and pic_id=%s";
		$this->query($sqlO,array($blogId));
		$this->query($sqlT,array($blogId,$picId));
	}

	public function addNewBlogPic($imageInfo,$blogId){
		$sqlO = "update hb_zj_blogs_pic set disrel=0 where blogid=%s";
		$this->query($sqlO,array($blogId));
	 	$addSql = "INSERT INTO `hb_zj_blogs_pic` (`pic_id`, `blogid`, `uid`, `filename`, `pic_width`, `pic_height`, `disrel`, `status`,`type`) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)";
		$data = array($imageInfo->Id,$blogId,$imageInfo->CreateBy,$imageInfo->Filename,$imageInfo->Width,$imageInfo->Height,1,1,3);
		$this->query($addSql,$data);
	}


	public function deleteBlog($blogId){
		$sql = "update hb_home_blog set status = 1 where blogid=%s";
		$this->query($sql,array($blogId));
	}

	public function getCateInfoByCateId($cateId){
		$data = array();
		$sql = "select * from hb_zj_blogs_category_old where status=1 and id=%s";
		$result = $this->query($sql,array($cateId));
		while($result->fetch_assoc()){
			$data['title'] = $result->getData('catename');
			$data['description'] = $result->getData('desc');
		}
		return $data;
	}

	public function editCate($conditions){
		if($conditions['type']){
			$sql = "insert into hb_zj_blogs_category_old (`uid`,`catename`,`catelevel`,`pubdate`,`status`,`parent`,`desc`) values(%s,%s,%s,%s,%s,%s,%s)";
			$this->query($sql,array('0',
									$conditions['title'],
									$conditions['cateId']?2:1,
									time(),
									1,
									$conditions['cateId'],
									$conditions['description']?$conditions['description']:'默认信息'

				));
		}else{
			$sql = "update hb_zj_blogs_category_old a set a.catename=%s,a.desc=%s where a.id=%s";
			$this->query($sql,array($conditions['title'],$conditions['description'],(int)$conditions['cateId']));
		}
	}

	public function delCate($cateId){
		$sqlO = "update hb_zj_blogs_category_old set status=%s where id=%s ";
		$sqlT = "update hb_zj_blogs_category_old set status=%s where parent=%s";
		$this->query($sqlO,array(0,$cateId));
		$this->query($sqlT,array(0,$cateId));
	}

	public function getUserByUid($uid){
		$sql = "select * from hb_common_member where uid = %s and status = 0";
		$result = $this->query($sql,array($uid))->fetch_assoc();
		return $result;
	}

	public function setUserBlogger($uid){
		$sql = "update hb_common_member set groupid = %s where uid = %s";
		$this->query($sql,array(self::FASHIONISTA_GROUP_ID,$uid));
	}

	private function getFirstCharter($str){
		if(empty($str)){return '';}
		$fchar=ord($str{0});

		if($fchar>=ord('A')&&$fchar<=ord('z')) return strtoupper($str{0});

		$s1=iconv('UTF-8','gb2312',$str);
		$s2=iconv('gb2312','UTF-8',$s1);
		$s=$s2==$str?$s1:$str;

		$asc=ord($s{0})*256+ord($s{1})-65536;

		if($asc>=-20319&&$asc<=-20284) return 'A';
		if($asc>=-20283&&$asc<=-19776) return 'B';
		if($asc>=-19775&&$asc<=-19219) return 'C';
		if($asc>=-19218&&$asc<=-18711) return 'D';
		if($asc>=-18710&&$asc<=-18527) return 'E';
		if($asc>=-18526&&$asc<=-18240) return 'F';
		if($asc>=-18239&&$asc<=-17923) return 'G';
		if($asc>=-17922&&$asc<=-17418) return 'H';
		if($asc>=-17417&&$asc<=-16475) return 'J';
		if($asc>=-16474&&$asc<=-16213) return 'K';
		if($asc>=-16212&&$asc<=-15641) return 'L';
		if($asc>=-15640&&$asc<=-15166) return 'M';
		if($asc>=-15165&&$asc<=-14923) return 'N';
		if($asc>=-14922&&$asc<=-14915) return 'O';
		if($asc>=-14914&&$asc<=-14631) return 'P';
		if($asc>=-14630&&$asc<=-14150) return 'Q';
		if($asc>=-14149&&$asc<=-14091) return 'R';
		if($asc>=-14090&&$asc<=-13319) return 'S';
		if($asc>=-13318&&$asc<=-12839) return 'T';
		if($asc>=-12838&&$asc<=-12557) return 'W';
		if($asc>=-12556&&$asc<=-11848) return 'X';
		if($asc>=-11847&&$asc<=-11056) return 'Y';
		if($asc>=-11055&&$asc<=-10247) return 'Z';
		return null;
	}
}

